package com.example.qjia.model;

import android.os.Handler;
import android.util.Log;

import com.example.qjia.Listener.LoadMessagesListener;
import com.example.qjia.Presenters.LoadMessagePresenter;
import com.example.qjia.basic.MyApplication;
import com.example.qjia.data.HolidayMessage;
import com.example.qjia.data.romotedata.AbsenceSlip;
import com.example.qjia.data.romotedata.ResponseData;
import com.example.qjia.util.retrofit.RetrofitNet;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rx.Observer;

/**
 * Created by ying_xiang on 2016/8/11.
 */
public class LoadMessageModelImp implements LoadMessageModel {
    private Handler handler=new Handler();
    @Override
    public void loadMessages(final LoadMessagesListener loadMessagesListener) {//加载请假列表
        final Map<String,String> params;
        params=new HashMap<>();
        System.out.println(MyApplication.getStudentId());
        params.put("userId",MyApplication.getStudentId()+"");
        params.put("userType",2+"");
        params.put("pageNum",1+"");
        params.put("pageSize",11+"");



          handler.postDelayed(new Runnable() {
              @Override
              public void run() {

                  /*AbsenceSlip a1=new AbsenceSlip();
                  a1.setAid(1);
                  a1.setReason("请假回家");
                  a1.setStatus(0);
                  a1.setTimeApply(new Date());
                  holidayMessages.add(a1);
                  AbsenceSlip a2=new AbsenceSlip();
                  a2.setAid(1);
                  a2.setReason("请假回家a");
                  a2.setStatus(0);
                  a2.setTimeApply(new Date());
                  holidayMessages.add(a2);
                  AbsenceSlip a3=new AbsenceSlip();
                  a3.setAid(1);
                  a3.setReason("请假回家");
                  a3.setStatus(0);
                  a3.setTimeApply(new Date());
                  holidayMessages.add(a3);
                  AbsenceSlip a4=new AbsenceSlip();
                  a4.setAid(1);
                  a4.setReason("请假回家");
                  a4.setStatus(0);
                  a4.setTimeApply(new Date());
                  holidayMessages.add(a4);
                  AbsenceSlip a5=new AbsenceSlip();
                  a5.setAid(1);
                  a5.setReason("请假回家");
                  a5.setStatus(0);
                  a5.setTimeApply(new Date());
                  holidayMessages.add(a5);
                  AbsenceSlip a6=new AbsenceSlip();
                  a6.setAid(1);
                  a6.setReason("请假回家");
                  a6.setStatus(0);
                  a6.setTimeApply(new Date());
                  holidayMessages.add(a6);*/


                  new RetrofitNet().getAbsenseSlipByPage(params,new Observer<ResponseData<List<AbsenceSlip>>>() {
                      @Override
                      public void onCompleted() {
                          //完成
                          System.out.println("finish");
                      }

                      @Override
                      public void onError(Throwable e) {
                          //异常
                          System.out.println("error");
                          Log.e("MAIN2",e.getLocalizedMessage()+"--"+e.getMessage());
                      }

                      @Override
                      public void onNext(ResponseData baseBean) {
                          //成功
                          ResponseData  p=baseBean;
                          System.out.println("有返回:");
                          List<AbsenceSlip>  holidayMessages=(List<AbsenceSlip>)(p.getData());
                          //System.out.println("请假理由："+holidayMessages.get(0).getReason());
                          System.out.println("p.getCode()="+p.getCode()+",    p.getMsg()="+p.getMsg());
                          if(p.getCode()==0) {            //表示提交成功


                              System.out.println(p.getCode() + "--" + p.getMsg());
                              loadMessagesListener.onSeccess(holidayMessages);
                          }else{
                              loadMessagesListener.failed();
                          }

                      }
                  });


              }
          },1000);




    }

    @Override
    public void loadMoreMessage(final Map<String,String>params,final LoadMessagesListener loadMessagesListener) {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                List<AbsenceSlip> holidayMessages=new ArrayList<>();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {




                        new RetrofitNet().getAbsenseSlipByPage(params,new Observer<ResponseData<List<AbsenceSlip>>>() {
                            @Override
                            public void onCompleted() {
                                //完成
                                System.out.println("finish");
                            }

                            @Override
                            public void onError(Throwable e) {
                                //异常
                                System.out.println("error");
                                Log.e("MAIN2",e.getLocalizedMessage()+"--"+e.getMessage());
                            }

                            @Override
                            public void onNext(ResponseData baseBean) {
                                //成功
                                ResponseData  p=baseBean;
                                System.out.println("有返回:");
                                List<AbsenceSlip>  holidayMessages=(List<AbsenceSlip>)(p.getData());
                                //System.out.println("请假理由："+holidayMessages.get(0).getReason());
                                System.out.println("p.getCode()="+p.getCode()+",    p.getMsg()="+p.getMsg());
                                if(p.getCode()==0) {            //表示提交成功


                                    System.out.println(p.getCode() + "--" + p.getMsg());
                                    loadMessagesListener.onSeccessloadmore(holidayMessages);
                                }else{
                                    loadMessagesListener.failed();
                                }

                            }
                        });


                    }
                },1000);



            }
        },2000);
    }
}
